//package com.pengshun.modules.jwt;
//
//import com.alibaba.fastjson.JSON;
//import com.pengshun.modules.api.mapper.PsUserInfoMapper;
//import com.pengshun.modules.api.coal.util.SystemPath;
//import com.pengshun.modules.common.BaseResult;
//import com.pengshun.modules.domain.User;
//import com.pengshun.modules.security.Authorization;
//import com.pengshun.modules.security.SelfUserDetailsService;
//import io.jsonwebtoken.Claims;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.lang.reflect.Method;
//
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.web.method.HandlerMethod;
//
///**
// * Created by Lisa on 2019/1/14.
// */
//
//@Component
//public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
//
//     @Autowired
//     private RedisTemplate<String, String> redisTemplate;
//
//     private final static String token = SystemPath.APP_TOKEN_PATH;
//
//     @Autowired
//     private  PsUserInfoMapper psUserInfoMapper;
//
//     @Autowired
//     SelfUserDetailsService userDetailsService;
//
//     @Override
//     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain,Object handler) throws ServletException, IOException {
//        String authHeader = request.getHeader("jwtToken");
////          String authHeader = "eyJhbGciOiJIUzUxMiJ9.eyJjZUlkIjoxLCJleHAiOjE1NzE0ODU1OTYsImNpSWQiOjF9.BybGVN1Pb90Kj2GPTHTll0Rqok40tpPhuIrv-nnxFJscwSy9VJbu7-HU9oomho5MDjynzYULlFd1j7yM_bqfwg";
//          JwtTokenUtil util = new JwtTokenUtil();
//          if (authHeader != null) {
//               final String authToken = authHeader;
//               HandlerMethod handlerMethod = (HandlerMethod) handler;
//               Method method = handlerMethod.getMethod();
//               if (method.getAnnotation(Authorization.class) == null) {
//                    return  ;
//               }
//               Claims claims = util.parseToken(authToken);
//               if (null == claims) {
//                    BaseResult result= new BaseResult(2,"token失效!");
//                    response.setCharacterEncoding("UTF-8");
//                    response.getWriter().write(JSON.toJSONString(result));
//                    return;
//               }
//               Long uiId = Long.valueOf(claims.get("uiId").toString());
//
//               String redisToken = redisTemplate.opsForValue().get(token + uiId);
////               if(redisToken!=null){
////                    redisToken=redisToken.substring(1,redisToken.length()-1);
////               }
//               if (StringUtils.isBlank(redisToken) || !redisToken.equals(authHeader)) {
//                    BaseResult result= new BaseResult(2,"token失效!");
//                    response.setCharacterEncoding("UTF-8");
//                    response.getWriter().write(JSON.toJSONString(result));
//                    return;
//               }
//               User employee = psUserInfoMapper.findUserById(uiId);
//               if (employee == null) {
//                    BaseResult result= new BaseResult(2,"token失效!");
//                    response.setCharacterEncoding("UTF-8");
//                    response.getWriter().write(JSON.toJSONString(result));
//                    return;
//               }
//               if (claims != null && StringUtils.isNotBlank(redisToken)) {
//                    User user = psUserInfoMapper.findUserByPhone(employee.getPhone());
//                    if(user==null){
//                         BaseResult result= new BaseResult(2,"token失效!");
//                         response.setCharacterEncoding("UTF-8");
//                         response.getWriter().write(JSON.toJSONString(result));
//                         return;
//                    }
//                    UserDetails userDetails = userDetailsService.loadUserByUsername(employee.getPhone());
//
//                    if (userDetails != null) {
//                         UsernamePasswordAuthenticationToken authentication =
//                                 new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//                         authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//
//                         SecurityContextHolder.getContext().setAuthentication(authentication);
//                    }
//               } else {
//                    BaseResult result= new BaseResult(2,"token失效!");
//                    response.setCharacterEncoding("UTF-8");
//                    response.getWriter().write(JSON.toJSONString(result));
//                    return;
////                throw new ExpiredJwtException(null, null, "token失效");
//               }
//          }
//
//          chain.doFilter(request, response);
//     }
//}
//
//
//
